import { createRouter, createWebHistory } from 'vue-router';
import Help from '../views/helpPage.vue';
import Home from '../views/index.vue';
import Login from '../views/login.vue';
import Details from '../components/details.vue'
import Notice from '../components/notice.vue'
import Order from '../views/Order.vue';
import { userStore } from "../store/user.js";

const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path: '/help',
    component: Help
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/details',
    component: Details
  }, {
    path: '/notice',
    component: Notice
  },
  {
    path: '/order',
    component: Order
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  const store = userStore(); // 获取 store 实例

  if (to.path === '/order') {
    if (store.isLog) {
      next(); // 已登录，允许访问
    } else {
      next('/login'); // 未登录，重定向到登录页
    }
  } else {
    next(); // 其他路由正常访问
  }
});
export default router;